package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.service.IActCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ActTitle;
import com.ruoyi.system.service.IActTitleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 命题管理Controller
 *
 * @author xuyk
 * @date 2021-11-29
 */
@Controller
@RequestMapping("/system/title")
public class ActTitleController extends BaseController
{
    private String prefix = "system/title";

    @Autowired
    private IActTitleService actTitleService;

    @Autowired
    private IActCategoryService actCategoryService;

    @RequiresPermissions("system:title:view")
    @GetMapping()
    public String title()
    {
        return prefix + "/title";
    }

    /**
     * 查询命题管理列表
     */
    @RequiresPermissions("system:title:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ActTitle actTitle)
    {
        startPage();
        List<ActTitle> list = actTitleService.selectActTitleList(actTitle);
        return getDataTable(list);
    }

    /**
     * 导出命题管理列表
     */
    @RequiresPermissions("system:title:export")
    @Log(title = "命题管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ActTitle actTitle)
    {
        List<ActTitle> list = actTitleService.selectActTitleList(actTitle);
        ExcelUtil<ActTitle> util = new ExcelUtil<ActTitle>(ActTitle.class);
        return util.exportExcel(list, "命题管理数据");
    }

    /**
     * 新增命题管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("categoryTree",actCategoryService.findWholeTree());
        return prefix + "/add";
    }

    /**
     * 新增保存命题管理
     */
    @RequiresPermissions("system:title:add")
    @Log(title = "命题管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ActTitle actTitle)
    {
        actTitle.setCreateBy(getLoginName());
        return toAjax(actTitleService.insertActTitle(actTitle));
    }

    /**
     * 修改命题管理
     */
    @RequiresPermissions("system:title:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ActTitle actTitle = actTitleService.selectActTitleById(id);
        mmap.put("categoryTree",actCategoryService.findWholeTree());
        mmap.put("actTitle", actTitle);
        return prefix + "/edit";
    }

    /**
     * 修改保存命题管理
     */
    @RequiresPermissions("system:title:edit")
    @Log(title = "命题管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ActTitle actTitle)
    {
        actTitle.setUpdateBy(getLoginName());
        return toAjax(actTitleService.updateActTitle(actTitle));
    }

    /**
     * 删除命题管理
     */
    @RequiresPermissions("system:title:remove")
    @Log(title = "命题管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(actTitleService.deleteActTitleByIds(ids));
    }


    /**
     * 通过GameId查询命题类别
     */
    @RequestMapping("/selectByGameId")
    @ResponseBody
    public AjaxResult selectByGameId(Long gameId)
    {
        return success(actTitleService.selectByGameId(gameId));
    }


    /**
     * 通过GameId和类别ID查询命题类别
     */
    @RequestMapping("/selectByGameIdAndCategoryId")
    @ResponseBody
    public AjaxResult selectByGameIdAndCategoryId(Long gameId,Long categoryId)
    {
        return success(actTitleService.selectByGameIdAndCategoryId(gameId,categoryId));
    }
}
